/*
 * Copyright (c) 2015 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kymjs.contacts.widget;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Rect;
import ohos.app.Context;

import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.MultimodalEvent;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 自定义文本输入框，增加清空按钮
 */
public class CustomEditText extends TextField implements Component.TouchEventListener {

    private Element mLeft, mTop, mRight, mBottom;

    private Rect mBounds;

    public CustomEditText(Context context) {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttrSet attrs, String defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomEditText(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setDrawable();
        // 增加文本监听器.
        addTextObserver(new TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                setDrawable();
            }
        });
        setTouchEventListener(this);
    }

    // 输入框右边的图标显示控制
    private void setDrawable() {
        if (length() == 0 || !isFocused()) {
            setAroundElements(mLeft, mTop, null, mBottom);
        } else {
            setAroundElements(mLeft, mTop, mRight, mBottom);
        }
    }

    @Override
    public void setAroundElements(Element left, Element top, Element right, Element bottom) {
        if (mLeft == null) {
            this.mLeft = left;
        }
        if (mTop == null) {
            this.mTop = top;
        }
        if (mRight == null) {
            this.mRight = right;
        }
        if (mBottom == null) {
            this.mBottom = bottom;
        }
        super.setAroundElements(left, top, right, bottom);

    }




//    @Override
//    protected void finalize() throws Throwable {
//        super.finalize();
//        this.mLeft = null;
//        this.mTop = null;
//        this.mRight = null;
//        this.mBottom = null;
//        this.mBounds = null;
//    }

//    @Override
//    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
//        super.onFocusChanged(focused, direction, previouslyFocusedRect);
//        setDrawable();
//    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (mRight != null && touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
            this.mBounds = mRight.getBounds();
//            mRight.getWidth();
            MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
            int eventX = (int) point.getX();
            int width = mBounds.getWidth();
            int right = getRight();
            int left = getLeft();
            if (eventX > (right - width * 2 - left)) {
                setText("");
//                touchEvent.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return true;
    }
}